package com.hetao.xx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.game.model.User;
import com.hetao.xx.service.HoleLevelService;
import com.hetao.xx.service.UserService;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HoleFragment extends Fragment {

    private View root;
    private Unbinder unbinder;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.hole_experience)
    TextView holeExperience;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.hole_level)
    TextView holeLevel;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.hole_money)
    TextView holeMoney;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.hole_exercise)
    Button holeExercise;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.hole_user_level)
    TextView holeUserLevel;


    private final HoleLevelService levelService = new HoleLevelService();
    private final UserService userLevelService = new UserService();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_main_hole, container, false);
            unbinder = ButterKnife.bind(this, root);
        }

        return root;
    }

    @SuppressLint("DefaultLocale")
    public void refreshUserInfo() {
        User user = Global.getInstant().getUser();
        holeExperience.setText(String.valueOf(user.info.experience));
        holeLevel.setText(String.format("%d级", user.info.holeLevel));
        holeMoney.setText(getMoney(user.info.money));
        holeExercise.setText(user.info.isExercise?R.string.hole_close_exercise:R.string.hole_exercise);

        holeUserLevel.setText(userLevelService.getName(user.info.userLevel));
    }

    @SuppressLint("DefaultLocale")
    private String getMoney(int money) {
        if ( money >= 100000000) {
            return String.format("%d亿",money/100000000);
        } else if(money >= 10000) {
            return String.format("%d万",money/10000);
        }

        return String.format("%d",money);
    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.hole_exercise, R.id.hole_up_hole, R.id.hole_up_user})
    public void onClick(View v) {
        User user = Global.getInstant().getUser();
        switch (v.getId()) {
            case R.id.hole_exercise:
                if (user.info.isExercise) {
                    //修炼结束
                    user.info.isExercise = false;
                    Date nowDate = new Date();
                    //时间经验
                    long total = (nowDate.getTime() - user.info.exerciseStart.getTime())/1000;
                    //聚灵阵加成
                    total = levelService.enhanceExperience(total, user.info.holeLevel);
                    user.info.experience += total;
                    Global.getInstant().toastMsg("结算经验  "+total);
                }else {
                    //修炼开始
                    user.info.exerciseStart = new Date();
                    user.info.isExercise = true;
                }
                refreshUserInfo();
                break;
            case R.id.hole_up_hole:
                if(levelService.upHoleLevel(user.info)) {
                    refreshUserInfo();
                }
                break;
            case R.id.hole_up_user:
                if(userLevelService.upUserLevel(user.info)) {
                    refreshUserInfo();
                }
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        refreshUserInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
